<?php
/**/
class Mod
{
	public $name = 'site';
	public $mkey = 'index';
	public $mid = 1;
	public $path = '';
	public $uri = '';
	
	public function __construct($is_sys = true)
	{
		$router = new Router();
		$run = $is_sys ? $GLOBALS['sys_run'] : $GLOBALS[APP_NAME . '_run'];
		if ( ! isset($run)) Message::error('$GLOBALS');
		if (isset($run['router'])) {
			if ($run['router']) {
				if ( ! empty($router->getParam(0))) $this->name = $router->getParam(0);
				if ( ! empty($router->getParam(1))) $this->mkey = $router->getParam(1);
				if ( ! empty($router->getParam(2))) $this->mid = $router->getParam(2);
			} else {
				if (isset($_GET[$run['modname']])) {
					$this->name = Unit::safe($_GET[$run['modname']]);
				}
				if (isset($_GET[$run['keyname']])) {
					$this->mkey = Unit::safe($_GET[$run['keyname']]);
				}
				if (isset($_GET[$run['idname']])) {
					$this->mkey = Unit::safe($_GET[$run['idname']]);
				}
			}
		} else {
			die('Config error!');
		}
		//$this->path = DATA_DIR . $this->name . DS;
		$this->path = MODULE_DIR . $this->name . DS;
		$this->uri = '/data/' . $this->name . '/';
	}
	
	public function run()
	{
		$file = MODULE_DIR . $this->name . '.php';
		if (file_exists($file)) {
			include $file;
			if (class_exists($this->name)) {
				$class = new $this->name();
				$func = $this->mkey;
				if (method_exists($class, $func)) {
					$class->$func($this->mid);
				} else {
					Message::error('method');
				}
			} else {
				Message::error('controller');
			}
		} else {
			Message::error('file');
		}
	}
	
	public function load($filename)
	{
		$res = '';
		if (is_array($filename)) {
			foreach ($filename as $type => $ary) {
				if ($type == 'css') {
					$this->uri .= 'css/';
					foreach ($ary as $item) {
						$res .= '<link rel="stylesheet" type="text/css" href="' . $this->uri  . $item . '.css" />' . chr(13);
					}
				} elseif ($type == 'js') {
					$this->uri  .= 'js/';
					foreach ($ary as $item) {
						$res .= '<script type="text/javascript" src="' . $this->uri  . $item . '.js"></script>' . chr(13);
					}
				} elseif ($type == 'image') {
					$this->uri  .= 'image/';
					foreach ($ary as $item) {
						$res .= '<img src="' . $this->uri  . $item . '" />' . chr(13);
					}
				}
			}
		}
		return $res;
		
	}
	
	/**
	 * 读取模板
	 * @param $tpl 模板文件名
	 * @param $data 传入模板的数据
	 * @param $dir 模板目录
	 * $data作为数据传入，在模板中可以使用$data变量来获取。$dir目录必须在views目录下。
	 */
	public function render($tpl, $data = array(), $dir = '')
	{
		$path = empty($dir) ? VIEW_DIR : VIEW_DIR . $dir . DS;
		$file = $path . $tpl . VIEW_SUFFIX;
		if (file_exists($file)) {
			include_once($file);
		}
		
	}
	
	// 根据pathinfo的第三个参数读取模板
	public function renderOn($data = array())
	{
		$router = new Router();
		$this->mid = empty($router->getParam(2)) ? 'index' : $router->getParam(2);
		$this->render($this->mid, $data);
	}
	
	public function create()
	{
		$res = [];
		$root = '/home/wwwroot/wind/laiyi.wang/';
		$dir = 'dev/views/'; 
		if (!is_dir($dir)) $res[] = mkdir($root . $dir);
		$file = $root . $dir . 'index.php';
		if(!file_exists($file)) $res[] = file_put_contents($file, '<?php');
		var_dump($res);
	}
	
}